package com.mw.wmimobile.gui.fragments;

import java.util.List;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.mw.wmimobile.datastorage.DataStorage;
import com.mw.wmimobile.datastorage.NewsSmallDto;
import com.wm.wmimobile.R;

public class NewsFragment extends FragmentActivity {

	private ListView listView;
	private List<NewsSmallDto> newsSmallDtoList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.news_activity_list);

		listView = (ListView) findViewById(R.id.NewsActivityListView);

		loadData();


		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					final int position, long id) {
//
//				Intent intent = new Intent(NewsFragment.this,
//						NewsActivity.class);
//				intent.putExtra("position", position);
//				startActivity(intent);
				
				AlertDialog.Builder alert = new AlertDialog.Builder(NewsFragment.this);
				alert.setTitle(DataStorage.getInstance().getNewsSmallList().get(position).getName());
				
				 WebView wv = new WebView(NewsFragment.this);
			        
			        wv.getSettings().setSupportZoom(true);
			        wv.getSettings().setBuiltInZoomControls(true);

			        
			        String html;
					try{
			        html =DataStorage.getInstance().getNewsSmallList().get(position).getText();
			        } catch (NullPointerException e){
			        	html = "BRAK OPISU";
			        }
			         
			       wv.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
			       //webView.loadData(res, "text/html", "UTF-8")
			       alert.setView(wv);
			       alert.show();

			}
		});
	}

	private void loadData() {
			newsSmallDtoList = DataStorage.getInstance().getNewsSmallList();
			showData();
	}

	private void showData() {
		listView.setAdapter(new NewsAdapter(NewsFragment.this, newsSmallDtoList));
	}

}
